package com.jzo2o.health.service.client;

import com.alibaba.cloud.sentinel.datasource.converter.JsonConverter;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.jzo2o.api.trade.NativePayApi;
import com.jzo2o.api.trade.dto.request.NativePayReqDTO;
import com.jzo2o.api.trade.dto.response.NativePayResDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author xd
 */
@Component
@Slf4j
public class NativePayClient {
    @Resource
    private NativePayApi nativePayApi;

    /**
     * @author xd
     * 远程调用支付申请
     * @param req {@code NativePayReqDTO} 支付申请实体类
     * @return {@code NativePayResDTO}支付申请响应结果
     */
    @SentinelResource(value = "payOrder",fallback = "payOrderFallback",blockHandler = "payOrderBlockHandler")
    public NativePayResDTO payOrder(NativePayReqDTO req){
        return nativePayApi.createDownLineTrading(req);
    }

    /**
     * @author xd
     * 异常降级方法
     * @param req {@code NativePayReqDTO} 支付申请实体类
     * @param t {@code Throwable} 异常类
     * @return null
     */
    public NativePayResDTO payOrderFallback(NativePayReqDTO req,Throwable t){
        log.error("远程调用支付服务出现异常,业务系统订单号为{}",req.getProductOrderNo(),t);
        return null;
    }

    /**
     * @author xd
     * 熔断降级方法
     * @param req {@code NativePayReqDTO} 支付申请实体类
     * @param e {@code BlockException} 异常类
     * @return null
     */
    public NativePayResDTO payOrderBlockHandler(NativePayReqDTO req, BlockException e) {
        log.error("远程调用支付服务请求已被熔断,业务系统订单号为{}",req.getProductOrderNo(),e);
        return null;
    }
}
